import http

from fastapi import Query

from models import base_model, data_models
from util.router import router


@router.get("/shop_login", tags=['商户'], summary='商户账户登录')
async def shop_l(name: str = Query(..., min_length=3, max_length=50),
                 pwd: str = Query(..., min_length=3, max_length=200)):
    '''
    用户注册路由
    :param name:
    :param pwd:
    :return:
    '''
    query = base_model.session.query(base_model.ShopInfo).filter(base_model.ShopInfo.shop_name == name).all()
    base_model.session.commit()
    if len(query) == 0:
        return {"code": http.HTTPStatus.UNAUTHORIZED, "msg": "此账号未注册"}
    else:
        if query[0].shop_status == 0:
            return {"code": http.HTTPStatus.UNAUTHORIZED, "msg": "登陆失败，账户未审核开通，请联系管理员"}
        if query[0].shop_status == 1:
            if pwd == query[0].shop_pwd:
                return {"code": http.HTTPStatus.OK, "msg": "登陆成功", "shop_name": query[0].shop_name}
            else:
                return {"code": http.HTTPStatus.UNAUTHORIZED, "msg": "账号或密码错误，登陆失败"}


@router.post("/shop_add", tags=['商户'], summary='添加用户')
async def user_re_add(args: data_models.UserInfo):
    '''
    添加用户路由
    :return:
    '''
    query = base_model.session.query(base_model.ShopInfo).filter(base_model.ShopInfo.shop_name == args.u_name).all()
    base_model.session.commit()
    if len(query) == 1:
        base_model.session.rollback()
        return {"code": http.HTTPStatus.OK, "msg": "添加商户失败", 'msg2': "商户重复", "return_code": 0}
    else:
        base_model.session.add(base_model.ShopInfo(shop_name=args.u_name,
                                                   shop_pwd=args.u_pwd
                                                   ))
        base_model.session.commit()
        return {"code": http.HTTPStatus.OK, "msg": "添加商户成功", 'u_name': args.u_name, "return_code": 1}
